/**
 * @author Beon
 * @date 2020/5/26
 * @Description: axios请求装饰器
*/
import Request from '../utils/axios';
import {AxiosRequestConfig} from 'axios';
import {ConfigInquirer} from '../inquirer/configInquirer';

export class AxiosDec {

    /**
     * 处理链接上的参数
     * @param url
     * @param urlParams
     * @returns {string}
     * @constructor
     */
    private static DealUrl(url: string, urlParams?: any): string {
        for (const key in urlParams) {
            const exp = new RegExp('\\${' + key + '}', 'g');
            url = url.replace(exp, urlParams[key]);
        }
        return url;
    }

    static Post(url: string, status?: boolean, baseConfig?: AxiosRequestConfig, baseUrl?: string): PropertyDecorator {
        return (target: any, propertyKey: string | symbol): void => {
            target[propertyKey] = (params: any, config?: AxiosRequestConfig, urlParams?: any, LABURL?: string) => Request.post((LABURL ? LABURL : ConfigInquirer[baseUrl]) + AxiosDec.DealUrl(url, urlParams), params, Object.assign(baseConfig, config), status);

        };
    }

    static Get(url: string, status?: boolean, baseConfig?: AxiosRequestConfig, baseUrl?: string): PropertyDecorator {
        return (target: any, propertyKey: string | symbol): void => {
            target[propertyKey] = (config?: AxiosRequestConfig, urlParams?: any, LABURL?: string) => Request.get((LABURL ? LABURL : ConfigInquirer[baseUrl]) + AxiosDec.DealUrl(url, urlParams), Object.assign(baseConfig, config), status);
        };
    }

    // static Put(url: string, status?: boolean, baseConfig?: AxiosRequestConfig, baseUrl?: string): PropertyDecorator {
    //     return (target: any, propertyKey: string | symbol): void => {
    //         target[propertyKey] = (params: any, config?: AxiosRequestConfig) => Request.put(ConfigInquirer[baseUrl] + url, params, Object.assign(baseConfig, config), status);
    //     };
    // }
    //
    // static Delete(url: string, status?: boolean, baseConfig?: AxiosRequestConfig, baseUrl?: string): PropertyDecorator {
    //     return (target: any, propertyKey: string | symbol): void => {
    //         target[propertyKey] = (config?: AxiosRequestConfig) => Request.delete(ConfigInquirer[baseUrl] + url, Object.assign(baseConfig, config), status);
    //     };
    // }
}
